package com.fengycx.templeate;

import cn.hutool.core.lang.Assert;
import com.fengycx.properties.GTPushProp;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import org.springframework.context.annotation.Configuration;

import java.util.Map;


@Configuration
public class GTPushTemplate {

    private GTPushProp gtPushProp;
    private UserApi userApi;
    private PushApi pushApi;


    public GTPushTemplate(GTPushProp gtPushProp, UserApi userApi, PushApi pushApi) {
        this.gtPushProp = gtPushProp;
        this.userApi=userApi;
        this.pushApi=pushApi;
    }


    /**
     * 绑定别名 alias
     * @param cid 客户端 ID，是个推 SDK在客户端设备上分配的唯一标识符，用于标识特定的客户端设备。CID 可以用于向特定的设备发送推送通知或消息
     * @param customerId
     */
    public void userAliasBind(String cid, String customerId){
        CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
        cidAliasListDTO.add(new CidAliasListDTO.CidAlias(cid,customerId));
        ApiResult<Void> apiResult = userApi.bindAlias(cidAliasListDTO);
        int code = apiResult.getCode();
        Assert.isTrue(code==0,"别名绑定失败");
    }

    /**
     * 根据别名进行单推
     * @param groupName 组名
     * @param transmission 消息主体
     * @param alias 别名 由customerId 和cid组成
     */
    public void pushSingleAlias(String groupName,String transmission,String alias){
        PushDTO<Audience> pushDTO = new PushDTO<>();
        pushDTO.setRequestId(System.currentTimeMillis()+"");
        pushDTO.setGroupName(groupName);

        //设置消息内容，使用的是透传方式
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(transmission);
        pushDTO.setPushMessage(pushMessage);

        //设置别名
        Audience audience = new Audience();
        audience.addAlias(alias);
        pushDTO.setAudience(audience);

        //发送消息
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);
        int code = apiResult.getCode();
        Assert.isTrue(code==0,"别名单推消息发送失败！");

    }
}
